package com.dcits.ftz.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dcits.ftz.entity.User;
import com.dcits.ftz.mapper.UserMapper;
import com.dcits.ftz.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kui.liu
 * @since 2025-06-06
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    private UserMapper userMapper;

    @Transactional(rollbackFor = Exception.class)
    public boolean insertBatch() {
        for (int i = 0; i < 1; i++) {
            User user = new User();
            user.setUsername("zhangsan" + Math.random());
            user.setPassword("123456");
            user.setName("张三");
            user.setCreateTime(new Date());
            userMapper.insert(user);
        }
        return true;
    }

    @Override
    public List<User> page(int pageNo) {

        Page<User> page = new Page<>(pageNo, 5);
        final List<User> records = userMapper.selectPage(page, null).getRecords();
        log.info("用户信息: {}", JSON.toJSONString(records));
        return records;
    }

    @Override
    public User findByUserName(String userName) {
        final User user = new User();
        user.setUsername("admin");
        user.setPassword("123456");
        return user;
    }

}
